package com.im.app.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Https {


//    private static String domain = "http://43.255.28.102:10002";
    private static String domain = "http://8.210.215.152:10002";
    public static  String SECRET = "tuoyun";
    public static String USER_REGISTER = domain+ "/auth/user_register";

    public static String USER_TOKEN = domain + "/auth/user_token";


    public static String ADD_FRIEND = domain + "/friend/add_friend";
    public static String ADD_GROUP = domain + "/group/create_group";
    public static String ADD_FRIEND_RESPONSE = domain + "/friend/add_friend_response";
    public static String Join_Group_URL = domain + "/group/invite_user_to_group";

    public static String SendWelcomeMessageURL = domain + "/msg/manage_send_msg";

//    private static String openIMAdmin = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSUQiOiJvcGVuSU1BZG1pbiIsIlBsYXRmb3JtIjoiV2ViIiwiZXhwIjoxNjcwNzUxNjY0LCJuYmYiOjE2NjI5NzUzNjQsImlhdCI6MTY2Mjk3NTY2NH0.P5Og3QRJbl4wlNuco26hfRjFU72rpveFEpwlEiyX4jc";
    public static String openIMAdmin = "";

    public final static String CHECKURL="http://wxapi2.jnoo.com/api/wxapijnoo418/e24e8489c357554a32de6f9664b0cae0?domain=";


    public static JSONObject post(String url, String jsonString){

        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token",openIMAdmin);
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        HttpEntity<Object> httpEntity = new HttpEntity<>(jsonString, headers);

        return client.postForEntity(url, httpEntity, JSONObject.class).getBody();
    }

//    public static JSONObject wxGet(String url){
//        RestTemplate client = new RestTemplate();
//        client.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
//        return client.getForEntity(url, JSONObject.class).getBody();
//    }





    public static JSONObject post(String url, String token, String jsonString){


        url = domain + url;

        RestTemplate client = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        headers.add("token",token);
        HttpEntity<Object> httpEntity = new HttpEntity<>(jsonString, headers);

        try {
            return client.postForEntity(url, httpEntity, JSONObject.class).getBody();
        } catch (RestClientException e){
        }

        return null;
    }

    private static final String UNKNOWN = "unknown";
    private static final String LOCALHOST = "127.0.0.1";
    private static final String LOCALHOST_IPV6 = "0:0:0:0:0:0:0:1";//客户端与服务器同为一台机器获取的ip有时候是ipv6格式表示的本地地址

    private static final String SEPARATOR = ",";

    public static String getIpAddr(HttpServletRequest request) {
        System.out.println(request.toString());
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            //ipAddress = request.getHeader("X-Forwarded-For");//有时候是大写，在于nginx.conf中的proxy_set_header如何配置了
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("X-Real-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || UNKNOWN.equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (LOCALHOST.equals(ipAddress)||LOCALHOST_IPV6.equals(ipAddress)) {
                    InetAddress inet = null;
                    try {
                        // 根据网卡取本机配置的IP
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            // "***.***.***.***".length()
            if (ipAddress != null && ipAddress.length() > 15) {
                if (ipAddress.indexOf(SEPARATOR) > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }



}
